package eu.telecombretagne.rsm.gui.frames.panels;

import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTextField;

import eu.telecombretagne.rsm.base.event.manager.EventParamsManager;
import eu.telecombretagne.rsm.gui.NameConsts;

public class DetailedSettingsPanel extends SettingsPanel {
	
	private static final long serialVersionUID = 8917738698125463989L;

	private JLabel labSettings;
	
	private JTextField fldProbaAckEvent;
	private JTextField fldProbaAlarmInitEvent;
	private JTextField fldProbaAppMessageEvent;
	private JTextField fldProbaAppMessageReturnEvent;
	private JTextField fldProbaFrameEvent;
	private JTextField fldProbaTimerExpiredEvent;
	
	private JTextField fldDelayAckEvent;
	private JTextField fldDelayAlarmInitEvent;
	private JTextField fldDelayAppMessageEvent;
	private JTextField fldDelayAppMessageReturnEvent;
	private JTextField fldDelayFrameEvent;
	private JTextField fldDelayTimerExpiredEvent;
	
	private EffectsPanel effectsAckEvent;
	private EffectsPanel effectsAlarmInitEvent;
	private EffectsPanel effectsAppMessageEvent;
	private EffectsPanel effectsAppMessageReturnEvent;
	private EffectsPanel effectsFrameEvent;
	private EffectsPanel effectsTimerExpiredEvent;
	
	/**
	 * The constructor of the DetailedSettingsPanel.
	 */
	public DetailedSettingsPanel(){
		setLayout(new GridLayout(6, 3));
		setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), NameConsts.DETAILEDSETTINGS));
		
		labSettings = new JLabel("Ack");
		fldProbaAckEvent = new JTextField(NameConsts.DEFAULTPROBABILITY);
		fldProbaAckEvent.setToolTipText(NameConsts.PROBABILITY);
		fldDelayAckEvent = new JTextField(NameConsts.DEFAULTDELAY);
		fldDelayAckEvent.setToolTipText(NameConsts.DELAYINMILLISECONDS);
		effectsAckEvent = new EffectsPanel();
		add(labSettings);
		add(createSubPanel(fldProbaAckEvent, fldDelayAckEvent));
		add(effectsAckEvent);
		
		labSettings = new JLabel("AlarmInit");
		fldProbaAlarmInitEvent = new JTextField(NameConsts.DEFAULTPROBABILITY);
		fldProbaAlarmInitEvent.setToolTipText(NameConsts.PROBABILITY);
		fldDelayAlarmInitEvent = new JTextField(NameConsts.DEFAULTDELAY);
		fldDelayAlarmInitEvent.setToolTipText(NameConsts.DELAYINMILLISECONDS);
		effectsAlarmInitEvent = new EffectsPanel();
		add(labSettings);
		add(createSubPanel(fldProbaAlarmInitEvent, fldDelayAlarmInitEvent));
		add(effectsAlarmInitEvent);
		
		labSettings = new JLabel("AppMessage");
		fldProbaAppMessageEvent = new JTextField(NameConsts.DEFAULTPROBABILITY);
		fldProbaAppMessageEvent.setToolTipText(NameConsts.PROBABILITY);
		fldDelayAppMessageEvent = new JTextField(NameConsts.DEFAULTDELAY);
		fldDelayAppMessageEvent.setToolTipText(NameConsts.DELAYINMILLISECONDS);
		effectsAppMessageEvent = new EffectsPanel();
		add(labSettings);
		add(createSubPanel(fldProbaAppMessageEvent, fldDelayAppMessageEvent));
		add(effectsAppMessageEvent);
		
		labSettings = new JLabel("AppMessageReturn");
		fldProbaAppMessageReturnEvent = new JTextField(NameConsts.DEFAULTPROBABILITY);
		fldProbaAppMessageReturnEvent.setToolTipText(NameConsts.PROBABILITY);
		fldDelayAppMessageReturnEvent = new JTextField(NameConsts.DEFAULTDELAY);
		fldDelayAppMessageReturnEvent.setToolTipText(NameConsts.DELAYINMILLISECONDS);
		effectsAppMessageReturnEvent = new EffectsPanel();
		add(labSettings);
		add(createSubPanel(fldProbaAppMessageReturnEvent, fldDelayAppMessageReturnEvent));
		add(effectsAppMessageReturnEvent);
		
		labSettings = new JLabel("Frame");
		fldProbaFrameEvent = new JTextField(NameConsts.DEFAULTPROBABILITY);
		fldProbaFrameEvent.setToolTipText(NameConsts.PROBABILITY);
		fldDelayFrameEvent = new JTextField(NameConsts.DEFAULTDELAY);
		fldDelayFrameEvent.setToolTipText(NameConsts.DELAYINMILLISECONDS);
		effectsFrameEvent = new EffectsPanel();
		add(labSettings);
		add(createSubPanel(fldProbaFrameEvent, fldDelayFrameEvent));
		add(effectsFrameEvent);
		
		labSettings = new JLabel("TimerExpired");
		fldProbaTimerExpiredEvent = new JTextField(NameConsts.DEFAULTPROBABILITY);
		fldProbaTimerExpiredEvent.setToolTipText(NameConsts.PROBABILITY);
		fldDelayTimerExpiredEvent = new JTextField(NameConsts.DEFAULTDELAY);
		fldDelayTimerExpiredEvent.setToolTipText(NameConsts.DELAYINMILLISECONDS);
		effectsTimerExpiredEvent = new EffectsPanel();
		add(labSettings);
		add(createSubPanel(fldProbaTimerExpiredEvent, fldDelayTimerExpiredEvent));
		add(effectsTimerExpiredEvent);
	}

	@Override
	public void readAllParams() {
		EventParamsManager.setUseSimplifiedValues(false);
		super.readAllParams();
	}
	
	@Override
	protected void readProbabilities(){
		setEventProbability(fldProbaAckEvent);
		setEventProbability(fldProbaAlarmInitEvent);
		setEventProbability(fldProbaAppMessageEvent);
		setEventProbability(fldProbaAppMessageReturnEvent);
		setEventProbability(fldProbaFrameEvent);
		setEventProbability(fldProbaTimerExpiredEvent);
	}
	
	@Override
	protected void readDelays(){	
		setEventDelay(fldDelayAckEvent);
		setEventDelay(fldDelayAlarmInitEvent);
		setEventDelay(fldDelayAppMessageEvent);
		setEventDelay(fldDelayAppMessageReturnEvent);
		setEventDelay(fldDelayFrameEvent);
		setEventDelay(fldDelayTimerExpiredEvent);
	}
	
	@Override
	protected void readEffects(){
		setEventEffects(effectsAckEvent);
		setEventEffects(effectsAlarmInitEvent);
		setEventEffects(effectsAppMessageEvent);
		setEventEffects(effectsAppMessageReturnEvent);
		setEventEffects(effectsFrameEvent);
		setEventEffects(effectsTimerExpiredEvent);
	}
	
	@Override
	protected Double setEventProbability(JTextField pTextField){
		Double probability = 1.0;
		
		if(getTextFromFieldAndParseToDouble(pTextField) != null){
			probability = getTextFromFieldAndParseToDouble(pTextField);
		}
		
		if(pTextField == fldProbaAckEvent){
			probability = EventParamsManager.setAckEventProbability(probability);
		} else if(pTextField == fldProbaAlarmInitEvent){
			probability = EventParamsManager.setAlarmInitEventProbability(probability);
		} else if(pTextField == fldProbaAppMessageEvent){
			probability = EventParamsManager.setAppMessageEventProbability(probability);
		} else if(pTextField == fldProbaAppMessageReturnEvent){
			probability = EventParamsManager.setAppMessageReturnEventProbability(probability);
		} else if(pTextField == fldProbaFrameEvent){
			probability = EventParamsManager.setFrameEventProbability(probability);
		} else if(pTextField == fldProbaTimerExpiredEvent){
			probability = EventParamsManager.setTimerExpiredEventProbability(probability);
		}
		
		pTextField.setText(String.valueOf(probability));
		
		return probability;
	}
	
	@Override
	protected Long setEventDelay(JTextField pTextField){
		Long delay = 100L;
		
		if(getTextFromFieldAndParseToDouble(pTextField) != null){
			delay = getTextFromFieldAndParseToLong(pTextField);
		}
		
		if(pTextField == fldDelayAckEvent){
			delay = EventParamsManager.setAckEventDelay(delay);
		} else if(pTextField == fldDelayAlarmInitEvent){
			delay = EventParamsManager.setAlarmInitEventDelay(delay);
		} else if(pTextField == fldDelayAppMessageEvent){
			delay = EventParamsManager.setAppMessageEventDelay(delay);
		} else if(pTextField == fldDelayAppMessageReturnEvent){
			delay = EventParamsManager.setAppMessageReturnEventDelay(delay);
		} else if(pTextField == fldDelayFrameEvent){
			delay = EventParamsManager.setFrameEventDelay(delay);
		} else if(pTextField == fldDelayTimerExpiredEvent){
			delay = EventParamsManager.setTimerExpiredEventDelay(delay);
		}
		
		pTextField.setText(String.valueOf(delay));
		
		return delay;
	}

	@Override
	protected void setEventEffects(EffectsPanel pEffectsPanel) {
		Boolean effects = ! pEffectsPanel.isNoneEnabled();
		Boolean duplication = pEffectsPanel.isDuplicationEnabled();
		
		if(pEffectsPanel == effectsAckEvent){
			EventParamsManager.setAckEventEffects(effects, duplication);
		} else if(pEffectsPanel == effectsAlarmInitEvent){
			EventParamsManager.setAlarmInitEventEffects(effects, duplication);
		} else if(pEffectsPanel == effectsAppMessageEvent){
			EventParamsManager.setAppMessageEventEffects(effects, duplication);
		} else if(pEffectsPanel == effectsAppMessageReturnEvent){
			EventParamsManager.setAppMessageReturnEventEffects(effects, duplication);
		} else if(pEffectsPanel == effectsFrameEvent){
			EventParamsManager.setFrameEventEffects(effects, duplication);
		} else if(pEffectsPanel == effectsTimerExpiredEvent){
			EventParamsManager.setTimerExpiredEventEffects(effects, duplication);
		}
	}
}